<template>
  <div class="serie-container">
    <div class="serie-top">
      <div class="serie-search"></div>
      <div class="serie-btn"><el-button type="primary" size="small" @click="isShowDialog = true">新增</el-button></div>
    </div>
    <el-scrollbar style="height: calc(100vh - 160px)" class="scrollbar" id="scroll-bar">
      <el-table :data="list">
        <el-table-column label="系列名称" prop="series_name"></el-table-column>
        <el-table-column label="系列简介" prop="series_comment"></el-table-column>
        <el-table-column label="最后修改时间" width="160" prop="data_update_time"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" @click="updSerie(scope.row.data_id, scope.row.series_name)" size="mini">
              修改
            </el-button>
            <el-button type="danger" @click="delSerie(scope.row.data_id)" size="mini">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-scrollbar>
    <paging ref="page" :func="getList"></paging>

      <el-dialog @closed="onClose"  title="提示" :visible.sync="isShowDialog">
        <el-form :model="params" :rules="rules" ref="dataParams" label-width="120px" class="demo-dataParams">
          <el-form-item label="系列名称" prop="series_name">
            <el-input v-model="params.series_name" placeholder="请输入系列名称"></el-input>
          </el-form-item>

          <el-form-item label="系列简介" prop="series_comment">
            <el-input v-model="params.series_comment" placeholder="请输入系列简介" type="textarea"></el-input>
          </el-form-item>

          <el-form-item label="艺术家名称" prop="art_name">
            <el-autocomplete
              v-model="params.art_name"
              :fetch-suggestions="querySearchAsync"
              value-key="name"
              placeholder="请输入艺术家名称"
              @select="handleSelect"
            ></el-autocomplete>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="isShowDialog = false">取 消</el-button>
          <el-button type="primary" @click="dialogConfirm">确 定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
import paging from '@/components/paging';
  import serieManage from '@/api/artSerie';
  import artisManage from '@/api/artis';
  export default {
    name: 'serie-view',
    components: {
      paging,
    },
    data() {
      return {
        list: [],
        isShowDialog: false,
        params: {
          art_id: '',
          art_name: '',
          series_name: '',
          series_comment: '',
        },
        menu_id: '',
        rules: {
          series_name: [{ required: true, message: '请输入系列名称', trigger: 'blur' }],
          art_name: [{ required: true, message: '请选择艺术家', trigger: 'change' }],
          series_comment: [{ required: true, message: '请输入系列简介', trigger: 'blur' }],
        },
      };
    },
    async mounted() {
      this.refresh();
    },
    methods: {
      refresh() {
        this.$refs.page.refresh();
      },
      search() {
        this.$refs.page.search();
      },
      async querySearchAsync(queryString, cb) {
        let res = await artisManage.getArtis({ key: queryString });
        console.log(res.item_list);
        cb(res.item_list);
      },
      handleSelect(item) {
        this.params.art_id = item.data_id;
      },
      async getList(callback) {
        let res = await serieManage.getSerie();
        this.list = res.item_list;
        callback(this.list, res.total_count);
      },
      updSerie(data_id, series_name) {
        this.updId = data_id;
        this.params.series_name = series_name;
        this.isShowDialog = true;
      },
      async delSerie(data_id) {
        this.$alert('是否确定删除系列', '删除', {
          confirmButtonText: '确定',
          callback: async (action) => {
            if (action === 'confirm') {
              await serieManage.delSerie(data_id);
              this.getList();
            }
          },
        });
      },
      async dialogConfirm() {
        if (!this.params.series_name) {
          return;
        }
        let res = null;
        if (this.updId) {
          res = await serieManage.updSerie(this.updId, this.params);
        } else {
          console.log(serieManage);
          res = await serieManage.setSerie(this.params);
        }
        if (res) {
          this.isShowDialog = false;
          this.getList();
        }
      },
      onClose() {
        this.updId = '';
        this.dataParams  = {
          art_id: '',
          art_name: '',
          series_name: '',
          series_comment: '',
        };
      },
    },
  };
</script>

<style lang="less" scoped>
  .serie-container {
    height: 100%;
    .serie-top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 12px;
      .serie-search {
        display: flex;
      }
    }
    .serie-list {
      display: flex;
      flex-flow: row wrap;
      width: 100%;
      height: 100%;
      .serie-item {
        width: calc(50% - 6px);
        height: 100px;
        display: flex;
        margin-bottom: 6px;
        border: 1px solid #f3f3f3;
        .item-content {
          margin: 10px;
          p {
            line-height: 40px;
          }
        }
      }
    }
  }
</style>
